package com.example.yls.demoa;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

/**
 * Created by yls on 2018/10/18.
 */

public class StudentAdapter extends ArrayAdapter {
    private final int resourceId;

    public StudentAdapter(@NonNull Context context, int textViewResourceId, @NonNull List objects) {
        super(context,textViewResourceId, objects);
        this.resourceId = textViewResourceId;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

        Student student = (Student) getItem(position);
        View view =
                LayoutInflater.from(getContext())
                        .inflate(resourceId,parent,false);
        ImageView studentImage = view.findViewById(R.id.student_image);
        TextView studentName = view.findViewById(R.id.student_name);
        studentImage.setImageResource(student.getImageId());
        studentName.setText(student.getName());

        return view;
    }
}
